package com.jacobson.core.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.jacobson.core.framework.annotation.IntentParameter
import com.jacobson.core.utils.ImmersiveExtensions
import com.jacobson.core.widget.logger.Logger
import com.jacobson.core.widget.logger.LoggerFactory

/**
 * ================================================
 * 基础Fragment
 * @author Wucz on 2018/7/16 14:54
 * ================================================
 */
abstract class BaseFragment : Fragment(), IFragment {
    /**
     * 日志输出
     */
    protected val logger: Logger = LoggerFactory.getLogger(javaClass)


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return initView(inflater, container, savedInstanceState)
    }

    override fun initView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        initView(savedInstanceState).takeIf { it != 0 }?.let {
            return inflater.inflate(it, container, false)
        }
        return null
    }

    override fun setData(data: Any?) {
    }

    override fun useEventBus(): Boolean {
        return true
    }

    override fun injectBundleParameter() {
        logger.d("BaseActivity->injectBundleParameter: ${this::class.simpleName}")
        val extras = arguments
        val injectActivity = this
        extras?.let {_ ->
            this.javaClass.declaredFields.forEach {
                it.getAnnotation(IntentParameter::class.java)?.run {
                    extras.get(key)?.run {
                        logger.d("setIntentParameter: ${it.name} = ${this}")
                        it.isAccessible = true
                        it.set(injectActivity, this)
                    } ?: run {
                        logger.w("require setIntentParameter: ${it.name} but not find value of key = $key")
                    }
                }
            }
        }
    }
}